//
//  TapGestureViewController.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2017/5/19.
//  Copyright © 2017年 zyy. All rights reserved.
//

import UIKit

class TapGestureViewController: ZyBaseViewController {

    var imageView:UIImageView?
    var index:NSInteger = 1
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //布局imageView
        self.layoutImageView()
        //1.创建轻拍手势
        self.tapGestureRecognizer()
        //2.创建清扫手势
        self.swipeGestureRecognizer()
        //3.创建长安手势
        self.longPressGestureRecognizer()
        //4.创建平移手势
        self.panGestureTecognizer()
        //5.创建捏合手势
        self.pinchGestureRecognizer()
        //6.创建 旋转手势
        self.rotationGestureRecognizer()
        //7.创建边缘手势.失败
//        self.screenEdgePanGestureRecognizer()
    }

    func layoutImageView() -> Void {
        let image = UIImage.init(named: "girl")
        imageView = UIImageView.init(image: image)
        self.view.addSubview(imageView!)
        imageView?.frame = CGRect(x:20.0, y:200.0, width:(image?.size.width)!, height:(image?.size.height)!)
        // 打开交互
        imageView?.isUserInteractionEnabled = true
    }
    
    func tapGestureRecognizer() -> Void {
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapGestureRecognizerClick(tap:)))
        //轻拍次数
        tap.numberOfTapsRequired = 1
        //轻拍手指个数
        tap.numberOfTouchesRequired = 1
        imageView?.addGestureRecognizer(tap)
    }
    
    func tapGestureRecognizerClick(tap:UITapGestureRecognizer) -> Void {
        self.nextImage()
    }
    
    func swipeGestureRecognizer() -> Void {
//        必须创建多个清扫手势 不然会覆盖
        let swipe1 = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeGestureRecognizerClick(swipe:)))
        swipe1.direction = UISwipeGestureRecognizerDirection.up
        imageView?.addGestureRecognizer(swipe1)
        
        let swipe2 = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeGestureRecognizerClick(swipe:)))
        swipe2.direction = UISwipeGestureRecognizerDirection.left
        imageView?.addGestureRecognizer(swipe2)
        
        let swipe3 = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeGestureRecognizerClick(swipe:)))
        swipe3.direction = UISwipeGestureRecognizerDirection.down
        imageView?.addGestureRecognizer(swipe3)
        
        let swipe4 = UISwipeGestureRecognizer.init(target: self, action: #selector(swipeGestureRecognizerClick(swipe:)))
        swipe4.direction = UISwipeGestureRecognizerDirection.right
        imageView?.addGestureRecognizer(swipe4)
    }
    
    func swipeGestureRecognizerClick(swipe:UISwipeGestureRecognizer) -> Void {
        if swipe.direction == UISwipeGestureRecognizerDirection.up {
            self.nextImage()
        }else if swipe.direction == UISwipeGestureRecognizerDirection.left {
            self.nextImage()
        }else if swipe.direction == UISwipeGestureRecognizerDirection.down {
            self.lastImage()
        }else if swipe.direction == UISwipeGestureRecognizerDirection.right {
            self.lastImage()
        }
    }
    
    func longPressGestureRecognizer() -> Void {
        let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressGestureRecognizerClick(longPress:)))
        imageView?.addGestureRecognizer(longPress)
        longPress.minimumPressDuration = 2
        longPress.allowableMovement = 1
    }
    
    func longPressGestureRecognizerClick(longPress:UILongPressGestureRecognizer) -> Void {
        if longPress.state == UIGestureRecognizerState.began {
            print("长按开始")
            UIView.animate(withDuration: 2, animations: {
                let image = self.imageView?.image
                self.imageView?.frame = CGRect(x:120.0, y:300.0, width:(image?.size.width)!, height:(image?.size.height)!)
            })
        }else if longPress.state == UIGestureRecognizerState.ended {
            print("长按结束")
            UIView.animate(withDuration: 2, animations: {
                let image = self.imageView?.image
                self.imageView?.frame = CGRect(x:10, y:65, width:(image?.size.width)!, height:(image?.size.height)!)
            })
        }
    }
    
    
    func panGestureTecognizer() -> Void {
        let pan = UIPanGestureRecognizer.init(target: self, action: #selector(panGestureTecognizerClick(pan:)))
        imageView?.addGestureRecognizer(pan)
    }
    
    func panGestureTecognizerClick(pan:UIPanGestureRecognizer) -> Void {
        let point:CGPoint = pan.translation(in: self.view)
        pan.view?.center = CGPoint(x:pan.view!.center.x + point.x, y:pan.view!.center.y + point.y)
        pan.setTranslation(CGPoint(x:0, y:0), in: self.view)
    }
    
    //捏合
    func pinchGestureRecognizer() -> Void {
        let pgr = UIPinchGestureRecognizer.init(target: self, action: #selector(pinchGestureRecognizerClick(pgr:)))
        imageView?.addGestureRecognizer(pgr)
    }
    
    func pinchGestureRecognizerClick(pgr:UIPinchGestureRecognizer) -> Void {
        pgr.view?.transform = CGAffineTransform.init(scaleX: pgr.scale, y: pgr.scale)
    }
    
    //旋转事件
    func rotationGestureRecognizer() -> Void {
        let rotation:UIRotationGestureRecognizer = UIRotationGestureRecognizer.init(target: self, action: #selector(rotationGestureRecognizerClick(rotation:)))
        imageView?.addGestureRecognizer(rotation)
    }
    
    func rotationGestureRecognizerClick(rotation:UIRotationGestureRecognizer) -> Void {
        imageView?.transform = CGAffineTransform.init(rotationAngle: rotation.rotation)
        //将旋转角度 置为 0
//        rotation.rotation = 0;
    }
    
//    //边缘滑动手势
//    func screenEdgePanGestureRecognizer() -> Void {
////        let screenpan:UIScreenEdgePanGestureRecognizer = UIScreenEdgePanGestureRecognizer.init(target: self, action: #selector(screenEdgePanGestureRecognizerClick(screen:)))
////        screenpan.edges = UIRectEdge.left
////        self.view?.addGestureRecognizer(screenpan)
////        screenpan.edges = UIRectEdge.right
////        screenpan.edges = UIRectEdge.top
//        
//        let screenEdgePanGesture = UIScreenEdgePanGestureRecognizer(target: self, action:#selector(screenEdgePanGestureRecognizerClick(screen:)))
//        screenEdgePanGesture.edges = UIRectEdge.left
//        self.view.addGestureRecognizer(screenEdgePanGesture)
//        
//    }
//    
//    func screenEdgePanGestureClick(gesture:UIScreenEdgePanGestureRecognizer)  {
//        print("screenEdgePanGestureClick")
//    }
//    
//    func screenEdgePanGestureRecognizerClick(screen:UIScreenEdgePanGestureRecognizer) -> Void {
//        print("边缘手势触发")
//        let randomColor:UIColor = UIColor.init(red: CGFloat((arc4random()%256)/255), green: CGFloat((arc4random()%256)/255), blue: CGFloat((arc4random()%256)/255), alpha: CGFloat((arc4random()%256)/255))
//        self.view.backgroundColor = randomColor
//    }
    
    func nextImage() -> Void {
        index += 1
        if  index == 9 {
            index = 1
        }
        
        let imageStr = "dm" + String(index)
        let image = UIImage.init(named: imageStr)
        UIView.animate(withDuration: 2) {
            self.imageView?.image = image
            self.imageView?.frame = CGRect(x:20.0, y:100.0, width:(image?.size.width)!, height:(image?.size.height)!)
        }
    }
    
    func lastImage() -> Void {
        index -= 1
        if  index == 0 {
            index = 8
        }
        
        let imageStr = "dm" + String(index)
        let image = UIImage.init(named: imageStr)
        UIView.animate(withDuration: 2) {
            self.imageView?.image = image
            self.imageView?.frame = CGRect(x:20.0, y:100.0, width:(image?.size.width)!, height:(image?.size.height)!)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
